/*
 Copyright (C) 2011  Matteo Magnani, Gabriele Nunziante
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class CRSearchResult  implements Comparable {
	
	/* Conversation metadata */
	public String name_user;
	public String screen_user;
	public String img_user;
	public String text_start;
	public String id_start;
	
	/* ranking parameters */
	public double score; /* text relevance */
	public double time_density; /* density of conversation */
	public int n_retweet; /* number of retweets */
	public int n_story; /* number of conversations of user - CURRENTLY NOT USED */
	public int n_reply; /* length of conversation (number of replies) */
	public int followers; /* number of followers */
	
	public double total;
	public Date time;
	public long time_start;
	public double totalS[];


	/* Maximum values (used to normalize parameters between 0 and 1) */
	private double max_score; /* text relevance */
	private double max_time_density; /* density of conversation */
	private int max_n_retweet; /* number of retweets */
	private int max_n_story; /* number of conversations of user - CURRENTLY NOT USED */
	private int max_n_reply; /* length of conversation (number of replies) */
	private int max_followers; /* number of followers */
	
	/* not used */
	private Date max_time;
	private long max_time_start;
	
	public CRSearchResult() {
		total = -1;
	}
	
	/**
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 **/
	public void total(double p_text, double p_retweet, double p_timeliness, double p_density, double p_followers) {
		//totalS = new double[8];
		total = 0;
		
		/* text score */
		total = (score/max_score)*p_text;
		//totalS[0] = (score/max_score)*p_text;

	    /* follower score */
		total += (followers/(double)max_followers)*p_followers;
		//totalS[1] = (followers/(double)max_followers)*p_followers;
		
	    /* number of conversations
	    if(n_story>0){
		    	total += ((n_reply/(double)n_story)/max_nn) * p_user;
		    	totalS[2] = ((n_reply/(double)n_story)/max_nn) * p_user;
		}
		 */
		
	   	/* density score */
		if (max_time_density > 0) {
			total += (time_density / (double)max_time_density) * p_density;
		//	totalS[3] = (time_density / (double)max_time_density) * p_density;
		}
		else {
			total += 0;
		//	totalS[3] = 0;
		}

		/* retweet score */
		if ( max_n_retweet > 0 ) {
			total += (n_retweet / (double)max_n_retweet) * p_retweet;
		//	totalS[4] = (n_retweet / (double)max_n_retweet) * p_retweet;
		}
		else {
			total += 0;
		//	totalS[4] = 0;
		}

	    /* timeliness score NOT USED HERE
	    Calendar cal = new GregorianCalendar();
		cal.setTime(time);
		time_start = cal.getTimeInMillis();
		total += (time_start/(double)max_time_start)*p_timeliness;
		//totalS[5] = (time_start/(double)max_time_start)*p_timeliness;
		 */
	}


	/**
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 * @param 
	 */
	public void setMax(double max_score, double max_time_density, int max_n_retweet, int max_n_story, int max_n_reply, int max_followers) {
		
		this.max_score = max_score; /* text relevance */
		this.max_time_density = max_time_density; /* density of conversation */
		this.max_n_retweet = max_n_retweet; /* number of retweets */
		this.max_n_story = max_n_story; /* number of conversations of user - CURRENTLY NOT USED */
		this.max_n_reply = max_n_reply; /* length of conversation (number of replies) */
		this.max_followers = max_followers; /* number of followers */
		
		/* TIMELINESS NOT USED HERE
		max_time = t_f_post;
		Calendar cal = new GregorianCalendar();
		cal.setTime(max_time);
		max_time_start = cal.getTimeInMillis();
		*/
	}

	public int compareTo(Object anotherStory) throws ClassCastException 
	{
	    if (!(anotherStory instanceof CRSearchResult))
	        throw new ClassCastException("A CRSearchResult object expected.");
	    
	  //  if( total > 0 ) {
		    double anotherTotal = ((CRSearchResult) anotherStory).total;
		    
		    if (this.total > anotherTotal)
		    	return -1;
		    else if (this.total == anotherTotal)
		    	return 0;
		    else
		    	return 1;
	 /*   }
	    else {
	    	double anotherScore = ((CRSearchResult) anotherStory).score;
		    
			int anotherReply = ((CRSearchResult) anotherStory).reply;
		    
		    if((this.score * (double) this.reply ) < (anotherScore * (double) anotherReply))
		    	return 1;
		    else if((this.score * (double) this.reply ) == (anotherScore * (double) anotherReply))
		    	return 0;
		    else
		    	return -1;
	    }*/
	}
	
}
